package com.cfuture.xiaozhi.smarthome.ui.contentfragment;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.Intent.ShortcutIconResource;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import com.cfuture.xiaozhi.smarthome.R;
import com.cfuture.xiaozhi.smarthome.adapter.RoomsTypeAdapter;

/**
 * 日光灯Fragment
 * 
 * @author cfuture_小智
 * @Description
 */
public class DayLightFragment extends Fragment implements OnClickListener {

	private ListView lvRooms;
	private Button btOn;
	private Button btOff;
	private ImageView ivOffFresh;
	private ImageView ivOnFresh;
	private int icons[] = { R.drawable.icon_bedroom1, R.drawable.icon_bedroom2,
			R.drawable.icon_kitchen, R.drawable.icon_parlour };
	private String roomType[] = { "房间1", "房间2", "厨房", "客厅" };
	private Context mContext;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mContext = getActivity();
		View v = inflater.inflate(R.layout.day_light, null);
		initView(v);
		return v;
	}

	private void initView(View v) {
		lvRooms = (ListView) v.findViewById(R.id.day_light_lv_center);
		RoomsTypeAdapter adpter = new RoomsTypeAdapter(mContext, roomType,
				icons);
		lvRooms.setAdapter(adpter);
		lvRooms.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Log.v("xiaozhi", "------------onClick");
			}
		});
		lvRooms.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View view,
					int position, long id) {
				new AlertDialog.Builder(mContext)
						.setMessage("是否把该电器发送到桌面快捷方式")
						.setIcon(R.drawable.ic_launcher)
						.setPositiveButton("确定",
								new DialogInterface.OnClickListener() {

									@Override
									public void onClick(DialogInterface dialog,
											int which) {
										Intent intentAddShortcut = new Intent(
												"com.android.launcher.action.INSTALL_SHORTCUT");
										intentAddShortcut.putExtra(
												Intent.EXTRA_SHORTCUT_NAME, "");
										ShortcutIconResource iconRes = Intent.ShortcutIconResource
												.fromContext(mContext,
														R.drawable.ic_launcher);
										intentAddShortcut
												.putExtra(
														Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
														iconRes);

										Intent intentLauncher = new Intent();
										// intentLauncher.setData();
										// intentLauncher.setComponent(new COM)
										intentLauncher
												.setClassName(
														"com.cfuture.xiaozhi.smarthome",
														"com.cfuture.xiaozhi.smarthome.ui.MainContentActivity");
										intentAddShortcut.putExtra(
												Intent.EXTRA_SHORTCUT_INTENT,
												intentLauncher);
										mContext.sendBroadcast(intentAddShortcut);
									}
								})
						.setNegativeButton("取消",
								new DialogInterface.OnClickListener() {

									@Override
									public void onClick(DialogInterface dialog,
											int which) {
										// TODO Auto-generated method stub

									}
								}).show();
				return true;
			}
		});
		btOff = (Button) v.findViewById(R.id.day_light_bt_off);
		btOff.setOnClickListener(this);
		btOn = (Button) v.findViewById(R.id.day_light_bt_on);
		btOn.setOnClickListener(this);
		ivOffFresh = (ImageView) v.findViewById(R.id.day_light_iv_off_fresh);
		ivOnFresh = (ImageView) v.findViewById(R.id.day_light_iv__on_fresh);
	}

	private void freshAnima(View v) {
		Animation anima = new RotateAnimation(0, 360, v.getWidth() / 2,
				v.getHeight() / 2);
		LinearInterpolator li = new LinearInterpolator();
		anima.setDuration(400);
		anima.setRepeatCount(3);
		anima.setInterpolator(li);
		v.startAnimation(anima);
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		// outState.putInt("mColorRes", mColorRes);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.view.View.OnClickListener#onClick(android.view.View)
	 */
	@Override
	public void onClick(View v) {
		int id = v.getId();
		switch (id) {
		case R.id.day_light_bt_off:
			freshAnima(ivOffFresh);
			break;
		case R.id.day_light_bt_on:
			// ivOffFresh
			freshAnima(ivOnFresh);
			break;
		default:
			break;
		}
	}

}
